home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / jrpacman.c < prev    next >
C/C++ Source or Header  |  2000-05-12  |  8KB  |  326 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7. ***************************************************************************/
  8.  
  9. #include "driver.h"
  10. #include "vidhrdw/generic.h"
  11.  
  12.  
  13.  
  14. unsigned char *jrpacman_scroll,*jrpacman_bgpriority;
  15. unsigned char *jrpacman_charbank,*jrpacman_spritebank;
  16. unsigned char *jrpacman_palettebank,*jrpacman_colortablebank;
  17. static int flipscreen;
  18.  
  19.  
  20. /***************************************************************************
  21.  
  22.   Convert the color PROMs into a more useable format.
  23.  
  24.   Jr. Pac Man has two 256x4 palette PROMs (the three msb of the address are
  25.   grounded, so the effective colors are only 32) and one 256x4 color lookup
  26.   table PROM.
  27.   The palette PROMs are connected to the RGB output this way:
  28.  
  29.   bit 3 -- 220 ohm resistor  -- BLUE
  30.         -- 470 ohm resistor  -- BLUE
  31.         -- 220 ohm resistor  -- GREEN
  32.   bit 0 -- 470 ohm resistor  -- GREEN
  33.  
  34.   bit 3 -- 1  kohm resistor  -- GREEN
  35.         -- 220 ohm resistor  -- RED
  36.         -- 470 ohm resistor  -- RED
  37.   bit 0 -- 1  kohm resistor  -- RED
  38.  
  39. ***************************************************************************/
  40. void jrpacman_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  41. {
  42.     int i;
  43.  
  44.  
  45.     for (i = 0;i < 32;i++)
  46.     {
  47.         int bit0,bit1,bit2;
  48.  
  49.  
  50.         bit0 = (color_prom[i] >> 0) & 0x01;
  51.         bit1 = (color_prom[i] >> 1) & 0x01;
  52.         bit2 = (color_prom[i] >> 2) & 0x01;
  53.         palette[3*i] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  54.         bit0 = (color_prom[i] >> 3) & 0x01;
  55.         bit1 = (color_prom[i+256] >> 0) & 0x01;
  56.         bit2 = (color_prom[i+256] >> 1) & 0x01;
  57.         palette[3*i + 1] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  58.         bit0 = 0;
  59.         bit1 = (color_prom[i+256] >> 2) & 0x01;
  60.         bit2 = (color_prom[i+256] >> 3) & 0x01;
  61.         palette[3*i + 2] = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  62.     }
  63.  
  64.     color_prom += 2*256;
  65.  
  66.     for (i = 0;i < 64*4;i++)
  67.     {
  68.         /* chars */
  69.         colortable[i] = color_prom[i];
  70.  
  71.         /* sprites */
  72.         if (color_prom[i]) colortable[i + 64*4] = color_prom[i] + 0x10;
  73.         else colortable[i + 64*4] = 0;
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. /***************************************************************************
  80.  
  81.   Start the video hardware emulation.
  82.  
  83. ***************************************************************************/
  84. int jrpacman_vh_start(void)
  85. {
  86.     if ((dirtybuffer = malloc(videoram_size)) == 0)
  87.         return 1;
  88.     memset(dirtybuffer,1,videoram_size);
  89.  
  90.     /* Jr. Pac Man has a virtual screen twice as large as the visible screen */
  91.     if ((tmpbitmap = osd_create_bitmap(Machine->drv->screen_width,2*Machine->drv->screen_height)) == 0)
  92.     {
  93.         free(dirtybuffer);
  94.         return 1;
  95.     }
  96.  
  97.     return 0;
  98. }
  99.  
  100.  
  101.  
  102. /***************************************************************************
  103.  
  104.   Stop the video hardware emulation.
  105.  
  106. ***************************************************************************/
  107. void jrpacman_vh_stop(void)
  108. {
  109.     free(dirtybuffer);
  110.     osd_free_bitmap(tmpbitmap);
  111. }
  112.  
  113.  
  114.  
  115. WRITE_HANDLER( jrpacman_videoram_w )
  116. {
  117.     if (videoram[offset] != data)
  118.     {
  119.         dirtybuffer[offset] = 1;
  120.  
  121.         videoram[offset] = data;
  122.  
  123.         if (offset < 32)    /* line color - mark whole line as dirty */
  124.         {
  125.             int i;
  126.  
  127.  
  128.             for (i = 2*32;i < 56*32;i += 32)
  129.                 dirtybuffer[i + offset] = 1;
  130.         }
  131.         else if (offset > 1792)    /* colors for top and bottom two rows */
  132.         {
  133.             dirtybuffer[offset & ~0x80] = 1;
  134.         }
  135.     }
  136. }
  137.  
  138.  
  139.  
  140. WRITE_HANDLER( jrpacman_palettebank_w )
  141. {
  142.     if (*jrpacman_palettebank != data)
  143.     {
  144.         *jrpacman_palettebank = data;
  145.         memset(dirtybuffer,1,videoram_size);
  146.     }
  147. }
  148.  
  149.  
  150.  
  151. WRITE_HANDLER( jrpacman_colortablebank_w )
  152. {
  153.     if (*jrpacman_colortablebank != data)
  154.     {
  155.         *jrpacman_colortablebank = data;
  156.         memset(dirtybuffer,1,videoram_size);
  157.     }
  158. }
  159.  
  160.  
  161.  
  162. WRITE_HANDLER( jrpacman_charbank_w )
  163. {
  164.     if (*jrpacman_charbank != data)
  165.     {
  166.         *jrpacman_charbank = data;
  167.         memset(dirtybuffer,1,videoram_size);
  168.     }
  169. }
  170.  
  171.  
  172. WRITE_HANDLER( jrpacman_flipscreen_w )
  173. {
  174.     if (flipscreen != (data & 1))
  175.     {
  176.         flipscreen = data & 1;
  177.         memset(dirtybuffer,1,videoram_size);
  178.     }
  179. }
  180.  
  181.  
  182. /***************************************************************************
  183.  
  184.   Draw the game screen in the given osd_bitmap.
  185.   Do NOT call osd_update_display() from this function, it will be called by
  186.   the main emulation engine.
  187.  
  188. ***************************************************************************/
  189. void jrpacman_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  190. {
  191.     int i,offs;
  192.  
  193.  
  194.     /* for every character in the Video RAM, check if it has been modified */
  195.     /* since last time and update it accordingly. */
  196.     for (offs = videoram_size - 1;offs >= 0;offs--)
  197.     {
  198.         if (dirtybuffer[offs])
  199.         {
  200.             int mx,my;
  201.  
  202.  
  203.             dirtybuffer[offs] = 0;
  204.  
  205.             /* Jr. Pac Man's screen layout is quite awkward */
  206.             mx = offs % 32;
  207.             my = offs / 32;
  208.  
  209.             if (my >= 2 && my < 60)
  210.             {
  211.                 int sx, sy;
  212.  
  213.                 if (my < 56)
  214.                 {
  215.                     sy = my;
  216.                     sx = mx+2;
  217.                     if (flipscreen)
  218.                     {
  219.                         sx = 35 - sx;
  220.                         sy = 55 - sy;
  221.                     }
  222.  
  223.                     drawgfx(tmpbitmap,Machine->gfx[0],
  224.                             videoram[offs] + 256 * *jrpacman_charbank,
  225.                         /* color is set line by line */
  226.                             (videoram[mx] & 0x1f) + 0x20 * (*jrpacman_colortablebank & 1)
  227.                                     + 0x40 * (*jrpacman_palettebank & 1),
  228.                             flipscreen,flipscreen,
  229.                             8*sx,8*sy,
  230.                             0,TRANSPARENCY_NONE,0);
  231.                 }
  232.                 else
  233.                 {
  234.                     if (my >= 58)
  235.                     {
  236.                         sy = mx - 2;
  237.                         sx = my - 58;
  238.                         if (flipscreen)
  239.                         {
  240.                             sx = 35 - sx;
  241.                             sy = 55 - sy;
  242.                         }
  243.  
  244.                         drawgfx(tmpbitmap,Machine->gfx[0],
  245.                                 videoram[offs],
  246.                                 (videoram[offs + 4*32] & 0x1f) + 0x20 * (*jrpacman_colortablebank & 1)
  247.                                         + 0x40 * (*jrpacman_palettebank & 1),
  248.                                 flipscreen,flipscreen,
  249.                                 8*sx,8*sy,
  250.                                 0,TRANSPARENCY_NONE,0);
  251.                     }
  252.                     else
  253.                     {
  254.                         sy = mx - 2;
  255.                         sx = my - 22;
  256.                         if (flipscreen)
  257.                         {
  258.                             sx = 35 - sx;
  259.                             sy = 55 - sy;
  260.                         }
  261.  
  262.                         drawgfx(tmpbitmap,Machine->gfx[0],
  263.                                 videoram[offs] + 0x100 * (*jrpacman_charbank & 1),
  264.                                 (videoram[offs + 4*32] & 0x1f) + 0x20 * (*jrpacman_colortablebank & 1)
  265.                                         + 0x40 * (*jrpacman_palettebank & 1),
  266.                                 flipscreen,flipscreen,
  267.                                 8*sx,8*sy,
  268.                                 0,TRANSPARENCY_NONE,0);
  269.                     }
  270.                 }
  271.             }
  272.         }
  273.     }
  274.  
  275.  
  276.     /* copy the temporary bitmap to the screen */
  277.     {
  278.         int scrolly[36];
  279.  
  280.  
  281.         for (i = 0;i < 2;i++)
  282.             scrolly[i] = 0;
  283.         for (i = 2;i < 34;i++)
  284.             scrolly[i] = -*jrpacman_scroll - 16;
  285.         for (i = 34;i < 36;i++)
  286.             scrolly[i] = 0;
  287.  
  288.         if (flipscreen)
  289.         {
  290.             for (i = 0;i < 36;i++)
  291.                 scrolly[i] = 224 - scrolly[i];
  292.         }
  293.  
  294.         copyscrollbitmap(bitmap,tmpbitmap,0,0,36,scrolly,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  295.     }
  296.  
  297.  
  298.     /* Draw the sprites. Note that it is important to draw them exactly in this */
  299.     /* order, to have the correct priorities. */
  300.     for (offs = spriteram_size - 2;offs > 2*2;offs -= 2)
  301.     {
  302.         drawgfx(bitmap,Machine->gfx[1],
  303.                 (spriteram[offs] >> 2) + 0x40 * (*jrpacman_spritebank & 1),
  304.                 (spriteram[offs + 1] & 0x1f) + 0x20 * (*jrpacman_colortablebank & 1)
  305.                         + 0x40 * (*jrpacman_palettebank & 1),
  306.                 spriteram[offs] & 1,spriteram[offs] & 2,
  307.                 272 - spriteram_2[offs + 1],spriteram_2[offs]-31,
  308.                 &Machine->drv->visible_area,
  309.                 (*jrpacman_bgpriority & 1) ? TRANSPARENCY_THROUGH : TRANSPARENCY_COLOR,
  310.                 (*jrpacman_bgpriority & 1) ? Machine->pens[0]     : 0);
  311.     }
  312.     /* the first two sprites must be offset one pixel to the left */
  313.     for (offs = 2*2;offs > 0;offs -= 2)
  314.     {
  315.         drawgfx(bitmap,Machine->gfx[1],
  316.                 (spriteram[offs] >> 2) + 0x40 * (*jrpacman_spritebank & 1),
  317.                 (spriteram[offs + 1] & 0x1f) + 0x20 * (*jrpacman_colortablebank & 1)
  318.                         + 0x40 * (*jrpacman_palettebank & 1),
  319.                 spriteram[offs] & 1,spriteram[offs] & 2,
  320.                 272 - spriteram_2[offs + 1],spriteram_2[offs]-30,
  321.                 &Machine->drv->visible_area,
  322.                 (*jrpacman_bgpriority & 1) ? TRANSPARENCY_THROUGH : TRANSPARENCY_COLOR,
  323.                 (*jrpacman_bgpriority & 1) ? Machine->pens[0]     : 0);
  324.     }
  325. }
  326.